/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mopso;

import java.util.ArrayList;

/**
 *
 * @author robson
 */
public class Particle implements Comparable, Cloneable {

    private ParticleData particleData;
    private boolean dominated;
    private ArrayList<ParticleData> pbests = new ArrayList<ParticleData>();
    private ParticleData leaderPbest;
    private Particle leader;
    private Problem problem;

    public Particle(Problem problem) {
        this.problem = problem;
        particleData = new ParticleData(problem);
    }

    public Particle(Problem problem, int index) {
        this.problem = problem;
        particleData = new ParticleData(problem, index);
    }

    public void calculateFitness() {
        for (int i = 0; i < problem.getFunctions().size(); i++) {
            particleData.getFitness()[i] = problem.getFunctions().get(i).function(getParticleData().getPositions());
        }

        particleData.setGValue(problem.getGValue());
    }

    public ParticleData getParticleData() {
        return particleData;
    }

    public void setParticleData(ParticleData particleData) {
        this.particleData = particleData;
    }

    public boolean isDominated() {
        return dominated;
    }

    public void setDominated(boolean dominated) {
        this.dominated = dominated;
    }

    public Object clone() {
        Particle clone = new Particle(problem);
        clone.dominated = this.dominated;
        clone.particleData = (ParticleData) this.particleData.clone();
        clone.pbests = (ArrayList<ParticleData>) this.pbests.clone();

//            clone.problem = (Problem) this.problem.clone();

        return clone;
    }

    public int compareTo(Object obj) {
        return this.particleData.compareTo(((Particle) obj).particleData);
    }

    public Particle getLeader() {
        return leader;
    }

    public void setLeader(Particle leader) {
        this.leader = leader;
    }

    public ParticleData getLeaderPbest() {
        return leaderPbest;
    }

    public void setLeaderPbest(ParticleData leaderPbest) {
        this.leaderPbest = leaderPbest;
    }

    public ArrayList<ParticleData> getPbests() {
        return pbests;
    }

    public void setPbests(ArrayList<ParticleData> pbests) {
        this.pbests = pbests;
    }
}
